import type { FieldRule } from 'vant'

// 提供校验规则
const mobileRules: FieldRule[] = [
  { required: true, message: '请输入手机号' },
  { pattern: /^1[3-9]\d{9}$/, message: '手机号不正确' }
]

const passwordRules: FieldRule[] = [
  { required: true, message: '请输入密码' },
  {
    pattern: /^.{8,15}$/,
    message: '密码长度必须在8到15个字符之间'
  },
  {
    validator: (value, _) => {
      console.log('Validating password:', value)
      // 确保 value 是字符串
      const actualValue =
        typeof value === 'object' && value !== null ? value.toString() : value
      if (!actualValue) return false

      const hasLetter = /[a-zA-Z]/.test(actualValue)
      const hasDigit = /\d/.test(actualValue)

      console.log(
        `hasLetter: ${hasLetter}, hasDigit: ${hasDigit}, value: ${actualValue}`
      )

      return hasLetter && hasDigit
    },
    message: '密码必须包含至少一个字母和一个数字'
  }
]

const codeRules: FieldRule[] = [
  { required: true, message: '请输入短信验证码' },
  { pattern: /^\d{6}$/, message: '验证码是6个数字' }
]

const emailRules: FieldRule[] = [
  { required: true, message: '请输入邮箱' },
  {
    pattern:
      /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/,
    message: '邮箱格式不正确'
  }
]

const nameRules: FieldRule[] = [
  { required: true, message: '请输入真实姓名' },
  { pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/, message: '中文字符2-16个字符' }
]

const idCardRules: FieldRule[] = [
  { required: true, message: '请输入身份证号' },
  {
    pattern:
      /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
    message: '身份证号格式不正确'
  }
]

const ageRules: FieldRule[] = [
  { required: true, message: '请输入年龄' },
  {
    pattern: /^(?:[1-9][0-9]?|1[01][0-9]|120)$/,
    message: '年龄必须是0-120之间的整数'
  }
]

export {
  mobileRules,
  emailRules,
  passwordRules,
  codeRules,
  nameRules,
  idCardRules,
  ageRules
}
